"""
# 对数变换实战 单词个数预测文章流行度
"""
# 使用在线新闻流行度数据集中经对数变换后的单词个数预测文章流行度
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score

news_df = pd.read_csv('../数据集/OnlineNewsPopularity.csv', delimiter=',', header=0)

news_df['log_n_tokens_content'] = np.log10(news_df[' n_tokens_content'] + 1)

m_orig = LinearRegression()
scores_orig = cross_val_score(m_orig, news_df[[' n_tokens_content']], news_df[' shares'], cv=10)
m_log = LinearRegression()
scores_log = cross_val_score(m_log, news_df[['log_n_tokens_content']], news_df[' shares'], cv=10)
print("R-squared score without log transform: {:.5} (+/- {:.5})\n"
      "R-squared score with log transform: {:.5} (+/- {:.5})".format(scores_orig.mean(), scores_orig.std(),
                                                                     scores_log.mean(), scores_log.std()))
